package com.ksm.zyz.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ksm.zyz.common.base.BaseController;
import com.ksm.zyz.sys.entity.OrderInstockModifyLog;
import com.ksm.zyz.sys.entity.ExtData;
import com.ksm.zyz.sys.entity.xinXibiao;
import com.ksm.zyz.sys.serverentity.Tongbu;
import com.ksm.zyz.sys.serverentity.sOrderInstockModifyLog;
import com.ksm.zyz.sys.serverservice.TongbuService;
import com.ksm.zyz.sys.serverservice.sOrderInstockModifyLogService;
import com.ksm.zyz.sys.serverservice.sXinXiBiaoService;
import com.ksm.zyz.sys.service.mySqlTongbuService;

/**
 * @author liuhao
 *
 */
@Controller
@RequestMapping("guodu")
public class guoduController extends BaseController<Tongbu> {

	@Autowired
	private TongbuService Service;
	@Autowired
	private sXinXiBiaoService xService;
	@Autowired
	private mySqlTongbuService mySqlTongbuService;
	@Autowired
	private sOrderInstockModifyLogService sOrderInstockModifyLogService;

	
	   @RequestMapping(value ="tongbu")
	    @ResponseBody
		public ExtData tongbu(int ming){
		   xinXibiao xinxi=xService.selectXinxi();
			
			if(xinxi.getNetUid()==0||xinxi.getNetUid()==null)
			{
			   System.out.println("不能进行数据同步,没有进行网上注册");
			}
			else
			{
				 //FIXME 
		    ming=xinxi.getNetUid();
		  //数据包的封装
			 // 分为  添加数据 删除 数据  和修改数据
			 // 例：//map("insert",insertmap<"OrderInstockModifyLog",List<OrderInstockModifyLog>>)
			 
			 
			 Map<String,List> insertmap=new HashMap<String,List>();
			 Map<String,List> deletemap=new HashMap<String,List>();
			 Map<String,List> updatemap=new HashMap<String,List>();
			 
			 //OrderInstockModifyLog
			 List<OrderInstockModifyLog> insertOrderInstockModifyLoglist=new ArrayList<OrderInstockModifyLog>();
			 List<OrderInstockModifyLog> deleteOrderInstockModifyLoglist=new ArrayList<OrderInstockModifyLog>();
			 List<OrderInstockModifyLog> updateOrderInstockModifyLoglist=new ArrayList<OrderInstockModifyLog>();

			 
			 
			 List<Tongbu> tlist= Service.selectAllNew(0);
			 int size=tlist.size();
			 for(int i=0;i<size;i++)
			  {
			    String operate=tlist.get(i).getOperate();
				String Biaoming=tlist.get(i).getBiaoming();
				String idkey=tlist.get(i).getIdkey();
								
				if(".".equals(Biaoming))
				{
								
				}
			 else if("order_instock_modify_log".equals(Biaoming))
				{
					if("inserted".equals(operate))
					{ 
					  sOrderInstockModifyLog sInfo=new sOrderInstockModifyLog();
					  int id=Integer.parseInt(idkey);
					  sInfo.setId(id);
					  OrderInstockModifyLog info=sOrderInstockModifyLogService.selectInfo(sInfo);
					  info.setSmId(ming);					  
					  insertOrderInstockModifyLoglist.add(info);
					}
					else if("delete".equals(operate))
					{
					  OrderInstockModifyLog info=new OrderInstockModifyLog();
					  int id=Integer.parseInt(idkey);
					  info.setId(id);
					  info.setSmId(ming);
					  deleteOrderInstockModifyLoglist.add(info);
					}
					else if("updated".equals(operate))
					{
					  sOrderInstockModifyLog sInfo=new sOrderInstockModifyLog();
					  int id=Integer.parseInt(idkey);
					  sInfo.setId(id);
					  OrderInstockModifyLog info=sOrderInstockModifyLogService.selectInfo(sInfo);
					  info.setSmId(ming);
					  updateOrderInstockModifyLoglist.add(info);
					}
				}
			
							
			  }
			 		
			 if(insertOrderInstockModifyLoglist.size()!=0)
			 {
			 insertmap.put("OrderInstockModifyLog", insertOrderInstockModifyLoglist);
			 }
			 if(deleteOrderInstockModifyLoglist.size()!=0)
			 {
			 deletemap.put("OrderInstockModifyLog", deleteOrderInstockModifyLoglist); 
			 }
			 if(updateOrderInstockModifyLoglist.size()!=0)
			 {
			 updatemap.put("OrderInstockModifyLog", updateOrderInstockModifyLoglist); 
			 }
			 
		
			 
			 
			 Map<String,Map> map=new HashMap<String, Map>();
			 if(insertmap.size()!=0)
			 map.put("insertmap", insertmap);
			 if(deletemap.size()!=0)
			 map.put("deletemap", deletemap);
			 if(updatemap.size()!=0)
			 map.put("updatemap", updatemap);
			 
			System.out.println(map);
			
	     
	   /*     ExtData extData = ExtData.getInstance();
	        List li=new ArrayList<Map>();
	        li.add(map);
	        extData.setList(li);
	        System.out.println(extData);*/
	        
	        
	        /*List li=new ArrayList<Map>();
	        li.add(map);
	        extData.setList(li);*/
	        
		}
			return extData;
	   

}
}
